package com.mrd.grpc;

import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

/**
 * @author jack
 * @data 2019/5/10 17:14
 */
public class GrpcServer {

    private Server server;

    private void start() throws IOException{
        this.server = ServerBuilder.forPort(8899).addService(new StudentServiceImpl()).build().start();
        System.out.println("server started!");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("关闭jvm");
            GrpcServer.this.stop();
        }));
        System.out.println("执行到这里");
    }

    private void stop() {
        if (null != this.server) {
            server.shutdown();
        }
    }

    private void aWaitTermination() throws InterruptedException {
        if (null != this.server) {
            this.server.awaitTermination();
        }
    }


    public static void main(String[] args) throws InterruptedException, IOException {
        GrpcServer server = new GrpcServer();

        server.start();
        server.aWaitTermination();
    }
}
